<template>
  <div class="drive-list">
    <RadioGroup @change="cattap" v-model:value="mrdriveId" name="radioGroup">
      <Radio v-for="(item, index) in driveList" :key="item.id" :value="item.id" class="flexRow">
        <div class="flexc">
          <div class="item_left" :title="item.name">{{ item.name }}</div>
          <div class="item_right">
            <a class="groupedite" @click.stop="del_Drive(item.id, index)">删除</a>
          </div>
        </div>
      </Radio>
    </RadioGroup>
  </div>
</template>
<script lang="ts">
  import { defineComponent, ref, Ref, onMounted } from 'vue';
  import { Radio, RadioGroup } from 'ant-design-vue';
  import { getDriveList, delDrive } from '/@/api/sys/gateway/gateway';
  import { useMessage } from '/@/hooks/web/useMessage';
  import Bus from '/@/utils/helper/eventbus';
  export default defineComponent({
    components: { Radio, RadioGroup },
    emits: ['sendDriveInfo'],
    setup(_, { emit }) {
      let driveList: Ref<any> = ref('');
      let mrdriveId = ref();
      const { createConfirm } = useMessage();
      let device_mode_id = ref();
      function del_Drive(id, index) {
        const obj = {
          id: id,
        };
        createConfirm({
          iconType: 'info',
          title: '确定删除吗',
          onOk() {
            delDrive(obj).then(() => {
              driveList.value.splice(index, 1);
              emit('sendDriveInfo', driveList.value[0]);
            });
          },
        });
      }
      //获取驱动列表
      async function getDriverList() {
        let res: any = await getDriveList({
          page: 1,
          page_size: 100000,
        });
        let list = res.list;
        driveList.value = list;
        let info = list?.find((item) => item.id == mrdriveId.value);
        if (info) {
          emit('sendDriveInfo', info);
        }
      }
      //点击驱动列表查询信息
      function cattap(e) {
        let arr = driveList.value.filter((value) => {
          return value.id == e.target.value;
        });
        emit('sendDriveInfo', arr[0]);
      }

      onMounted(() => {
        Bus.$on('send_device_type_id', (e) => {
          device_mode_id.value = e.device_mode_id;
          mrdriveId.value = e.default_drive_id;
          getDriverList();
        });
      });
      return {
        driveList,
        mrdriveId,
        cattap,
        del_Drive,
      };
    },
  });
</script>

<style lang="less" scoped>
  .drive-list {
    width: 100%;
    max-height: 300px;
    display: flex;
    flex-direction: column;
    border: 1px solid #efefef;
    padding: 10px;
    box-sizing: border-box;
    overflow-y: visible;
    overflow-x: hidden !important;
  }
  .drive-list .flexRow {
    width: 100%;
    display: flex;
    align-items: center;
    margin-bottom: 6px;
  }
  .drive-list .flexRow .flexc {
    width: 88%;
    display: flex;
    align-items: center;
    justify-content: space-between;
  }

  .drive-list .ant-list-item-meta-title {
    display: flex;
    justify-content: space-between;
    padding: 0px 10px;
    box-sizing: border-box;
  }
  .drive-list .item_left {
    padding: 5px;
    box-sizing: border-box;
    width: 260px;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    border: 1px solid #efefef;
  }
  .drive-list .item_right {
    flex: 1;
    display: flex;
  }

  .drive-list .item_right .groupedite {
    margin-left: 10px;
  }
</style>
